package cn.jeefast.rest.service.impl;

import cn.jeefast.common.entity.*;
import cn.jeefast.common.utils.R;
import cn.jeefast.rest.dao.*;
import cn.jeefast.rest.service.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import jdk.nashorn.internal.ir.EmptyNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author theodo
 * @since 2019-12-30
 */
@Service
public class MbPatientServiceImpl extends ServiceImpl<MbPatientDao, MbPatient> implements MbPatientService {

    @Autowired
    private MbPatientDao mbPatientDao;

    @Autowired
    private MbResearchStepDao researchStepDao;

    @Autowired
    private MbResearchDataDao mbResearchDataDao;

    @Autowired
    private MbResearchSetpDataDetailService researchSetpDataDetailService;

    @Autowired
    private MbResearchService mbResearchService;

    @Autowired
    private MbDoctorDao doctorDao;

    @Override
    @Transactional
    public R addPatient(MbPatient patient) {
        //获取项目ID
        patient.setTimes(new Date());
        String pid = patient.getProjId().toString();
        if(pid.length()==1){
            pid = "0"+pid;
        }
        //生成患者ID
        int i = (int) ((Math.random() * 9 + 1) * 100000);
        //拼接项目ID+患者ID
        String uid = pid+i;
        MbPatient mbPatient = mbPatientDao.selectById(uid);
        while(mbPatient!=null){
            uid = pid+(int) ((Math.random() * 9 + 1) * 100000);
            mbPatient = mbPatientDao.selectById(uid);
        }
        patient.setId(Long.parseLong(uid));
        //插入患者基础信息
        mbPatientDao.insertPatient(patient);
        //获取研究组模板并插入记录
        EntityWrapper we = new EntityWrapper();
        we.eq("res_id",patient.getResId());
        List<MbResearchStep> list = researchStepDao.selectList(we);
        for (MbResearchStep mrs:list) {
            MbResearchData mbResearchData = new MbResearchData();
            mbResearchData.setPatientId(patient.getId());
            mbResearchData.setResStepId(mrs.getStepId());
            mbResearchData.setDocId(patient.getDocId());
            //TODO 时间计算有问题 超出一个月计算时间不正确
            mbResearchData.setStartTime(new Date(new Date().getTime()+mrs.getMark1()*86400000));
            mbResearchData.setEndTime(new Date(new Date().getTime()+mrs.getMark2()*86400000));
            mbResearchDataDao.insert(mbResearchData);
            //插入模板详情
            for (int j=0;j<9;j++){
                MbResearchSetpDataDetail mbResearchSetpDataDetail = new MbResearchSetpDataDetail();
                mbResearchSetpDataDetail.setDdType(j+1);
                mbResearchSetpDataDetail.setDataDetailId(mbResearchData.getDataId());
                researchSetpDataDetailService.insert(mbResearchSetpDataDetail);
            }
        }
        return R.ok("").put("data",null);
    }

    /**
     * 医生负责的患者列表
     * @param docId
     * @param page
     * @param limit
     * @return
     */
    @Override
    public R patientList(Long docId, Integer page, Integer limit,String keyword,Long[] resId,Long[] stepId,String[] dig) {
        //查询基础信息
        List<Map> maps = mbPatientDao.docCharge(docId, page, limit,keyword,resId,stepId,dig);
        List<Map> newMap = new ArrayList<>();
        for (Map m:maps) {
            //增加研究周期数据
            EntityWrapper ew = new EntityWrapper();
            ew.eq("patient_id",m.get("id"));
            ew.orderBy("data_id",true);
            List<MbResearchData> list = mbResearchDataDao.selectList(ew);
            for (MbResearchData mrd:list) {
                MbResearchStep mbResearchStep = researchStepDao.selectById(mrd.getResStepId());
                mrd.setStepName(mbResearchStep.getStepName());
            }
            Map map = new HashMap();
            map.put("patient",m);
            map.put("research",list);
            newMap.add(map);
        }
        List<Map> counts = mbPatientDao.docChargeCount(docId,keyword,resId,stepId,dig);
        Map result = new HashMap();
        result.put("dataList",newMap);
        result.put("count",counts.size());
        return R.ok("").put("data",result);
    }

    @Override
    public R patientData(Long pid) {
        //根据患者ID查询已审核过的阶段
        EntityWrapper ew = new EntityWrapper();
        ew.eq("patient_id",pid);
        ew.eq("state",3);
        List<MbResearchData> list = mbResearchDataDao.selectList(ew);
        //获取周期列表
        List<String> r = new ArrayList();
        List<List> l = new ArrayList<>();
        MbPatient mbPatient = mbPatientDao.selectById(pid);
        MbResearch mbResearch = mbResearchService.selectById(mbPatient.getResId());
        for (MbResearchData mrd:list) {
            EntityWrapper ew1 = new EntityWrapper();
            ew1.eq("data_detail_id",mrd.getDataId());
            List<MbResearchSetpDataDetail> list1 = researchSetpDataDetailService.selectList(ew1);
            for (MbResearchSetpDataDetail mbsdd:list1) {
                //1:ALB 2:CPR 3:TCHO 4:TG 5:LDL-C 6:HDL-C 7:SCR 8:BUN 9:P
                if(mbsdd.getDdType()==1){
                    mbsdd.setName("ALB");
                }else if(mbsdd.getDdType()==2){
                    mbsdd.setName("CPR");
                }else if(mbsdd.getDdType()==3){
                    mbsdd.setName("TCHO");
                }else if(mbsdd.getDdType()==4){
                    mbsdd.setName("TG");
                }else if(mbsdd.getDdType()==5){
                    mbsdd.setName("LDL-C");
                }else if(mbsdd.getDdType()==6){
                    mbsdd.setName("HDL-C");
                }else if(mbsdd.getDdType()==7){
                    mbsdd.setName("SCR");
                }else if(mbsdd.getDdType()==8){
                    mbsdd.setName("BUN");
                }else if(mbsdd.getDdType()==9){
                    mbsdd.setName("P");
                }
            }
            l.add(list1);
            MbResearchStep mbResearchStep = researchStepDao.selectById(mrd.getResStepId());
            r.add(mbResearchStep.getStepName());
        }
        Map map = new HashMap();
        map.put("step",r);
        map.put("data",l);
        map.put("patient",mbPatient);
        map.put("research",mbResearch.getResName());
        return R.ok("").put("data",map);
    }

    @Override
    public R excel(Long pid, String img1, String img2) {
        //生成Excel

        return null;
    }

    @Override
    public R exportList(Long[] ids) {
        //判断ids中有几种患者类型
        List<MbPatient> list1 = mbPatientDao.selectPatientByIds(ids);
        List<Map<Long,String>> resList = new ArrayList<>();
        for (MbPatient mp:list1) {
            MbResearch mbResearch = mbResearchService.selectById(mp.getResId());
            Map map = new HashMap();
            map.put(mp.getResId(),mbResearch.getResName());
            resList.add(map);
        }
        //按每组去循环,每组导出的为一个Excel
        for (Map m:resList) {

        }
//        List<Map> list = new ArrayList<Map>();
//        //根据患者ID查询基础信息
//        for (Long id:ids) {
//            Map map = new HashMap();
//            MbPatient mbPatient = mbPatientDao.selectById(id);
//            map.put("id",mbPatient.getId());
//            map.put("name",mbPatient.getPatName());
//            MbDoctor mbDoctor = doctorDao.selectById(mbPatient.getDocId());
//            map.put("doc",mbDoctor);
////            map.put()
//        }
        return null;
    }

    @Override
    public List<Map> diagnosisList() {
        return mbPatientDao.diagnosisList();
    }

    @Override
    public List<Long> selectResIdByPatId(Long[] ids) {
        return mbPatientDao.selectResIdByPatId(ids);
    }

}
